package com.jiajia.study.product.controller;

import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.R;
import com.jiajia.study.product.entity.AttrEntity;
import com.jiajia.study.product.entity.AttrGroupEntity;
import com.jiajia.study.product.service.AttrGroupService;
import com.jiajia.study.product.service.CategoryService;
import com.jiajia.study.product.vo.AttrGroupRelationVo;
import com.jiajia.study.product.vo.AttrGroupWithAttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author zjj
 * @email zjj@qq.com
 * @date 2025-06-09 15:37:17
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrId){
        List<AttrEntity> result =  attrGroupService.getRelationAttr(attrId);
        return R.ok().put("data",result);
    }

    @RequestMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupWithAttrVO> result = attrGroupService.getAttrgroupWithAttrs(catelogId);
        return R.ok().put("data",result) ;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据分组id查询属性分组列表
     * @param params        分页查询参数
     * @param catelogId     三级分类id
     * @return              分组分页信息
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 查询分组未关联属性
     * @param attrGroupId   分组id
     * @return              未关联属性列表
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId")Long attrGroupId,@RequestParam Map<String,Object> params){
        PageUtils page = attrGroupService.attrNoRelation(attrGroupId,params);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrGroupService.saveRelation(attrGroupRelationVos);
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        attrGroupService.deleteRelation(relationVos);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
